package com.example.management.system.service.impl

import com.example.management.system.dao.RoleDao
import com.example.management.system.dao.UserRoleDao
import com.example.management.common.domain.RoleMenuDO
import com.example.management.system.dao.RoleMenuDao
import com.example.management.system.dao.UserDao
import com.example.management.system.domain.RoleDO
import com.example.management.system.service.RoleService
import org.springframework.beans.factory.annotation.Autowired
import org.springframework.stereotype.Service
import org.springframework.transaction.annotation.Transactional

import java.util.ArrayList
import java.util.HashMap


@Service
class RoleServiceImpl : RoleService {

    @Autowired
    internal var roleMapper: RoleDao? = null
    @Autowired
    internal var roleMenuMapper: RoleMenuDao? = null
    @Autowired
    internal var userMapper: UserDao? = null
    @Autowired
    internal var userRoleMapper: UserRoleDao? = null

    override fun list(): List<RoleDO> {
        return roleMapper!!.list(HashMap<String, Any>(16))
    }


    override fun list(userId: Long?): List<RoleDO> {
        val rolesIds = userRoleMapper!!.listRoleId(userId)
        val roles = roleMapper!!.list(HashMap<String, Any>(16))
        for (roleDO in roles) {
            roleDO.roleSign = ("false")
            for (roleId in rolesIds) {
                if (roleDO.roleId == roleId) {
                    roleDO.roleSign = ("true")
                    break
                }
            }
        }
        return roles
    }

    @Transactional
    override fun save(role: RoleDO): Int {
        val count = roleMapper!!.save(role)
        val menuIds = role.menuIds
        val roleId = role.roleId
        val rms = ArrayList<RoleMenuDO>()
        for (menuId in menuIds!!) {
            val rmDo = RoleMenuDO()
            rmDo.roleId = (roleId)
            rmDo.menuId = (menuId)
            rms.add(rmDo)
        }
        roleMenuMapper!!.removeByRoleId(roleId)
        if (rms.size > 0) {
            roleMenuMapper!!.batchSave(rms)
        }
        return count
    }

    @Transactional
    override fun remove(id: Long?): Int {
        val count = roleMapper!!.remove(id)
        userRoleMapper!!.removeByRoleId(id)
        roleMenuMapper!!.removeByRoleId(id)
        return count
    }

    override operator fun get(id: Long?): RoleDO {
        return roleMapper!!.get(id)
    }

    override fun update(role: RoleDO): Int {
        val r = roleMapper!!.update(role)
        val menuIds = role.menuIds
        val roleId = role.roleId
        roleMenuMapper!!.removeByRoleId(roleId)
        val rms = ArrayList<RoleMenuDO>()
        for (menuId in menuIds!!) {
            val rmDo = RoleMenuDO()
            rmDo.roleId =(roleId)
            rmDo.menuId = (menuId)
            rms.add(rmDo)
        }
        if (rms.size > 0) {
            roleMenuMapper!!.batchSave(rms)
        }
        return r
    }

    override fun batchremove(ids: Array<Long>): Int {
        return roleMapper!!.batchRemove(ids)
    }

    companion object {

        val ROLE_ALL_KEY = "\"role_all\""

        val DEMO_CACHE_NAME = "role"
    }

}
